package com.cn.wanxi.service;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.util.IO;
import com.cn.wanxi.util.Path;
import com.cn.wanxi.util.Tool;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;

import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NavService {


    public NavModel[] getNavModels() {

//        NavDao navModel = new NavDao();
//        navModel.setTitle("网站首页");
//        navModels[0] = navModel;
//        NavDao navModel1 = new NavDao();
//        navModel1.setTitle("公司简介");
//        navModels[1] = navModel1;
//        NavDao navModel2 = new NavDao();
//        navModel2.setTitle("新闻动态");
//        navModels[2] = navModel2;
//        数组的静态定义
        NavDao navDao = new NavDao();
        int leng = navDao.getTitles().length;
        NavModel[] navModels = new NavModel[leng];
        for (int i = 0; i < leng; i++) {
            NavModel navModel = new NavModel();
            navModel.setTitle(navDao.getTitles()[i]);
            navModels[i] = navModel;
        }
        return navModels;
    }

    /**
     * 1.判断当前是否有文件
     * 2.创建文件
     * * 3.写入数据
     * * 4.读取数据
     * * 5.封装数据
     *
     * @return
     */
    public List<Object> getNavModelList() {
//        List<Object> list = new ArrayList<>();
//        1.创建文件
//        IO.creatNote(PATH, FILENAME);
//        2.写入数据

        setDaoData();
//        3.读取数据
        String result = IO.readNote();
//        4.封装数据


        return setData(result);
    }

    private void setDaoData() {
        List<String> list = new ArrayList<>();
        NavDao navDao = new NavDao();
        list.add(Arrays.toString(navDao.getTitles()));
        list.add(Arrays.toString(navDao.getHrefs()));
        IO.writeNote(list, Path.FILE_NAV.getPath());
    }

    public static void main(String[] args) {
        System.out.println(Path.PATH.getPath());
    }

    /**
     * 封装数据
     */
    private List<Object> setData(String result) {
//        split通过分割字符将字符串转换为字符串数组
        String[] results = result.split(Path.SPLIT.getPath());
        String title = results[0];
//        字符串去掉中括号
//        String replace = title.replace("[", "");
//        String replace1 = replace.replace("]", "");
//        String replace2 = replace1.replace(" ", "");
        String href = results[1];
        List<Object> list = new ArrayList<>();
//        NavDao navDao = new NavDao();
        String[] titles = Tool.replace(title).split(",");
        String[] hrefs = Tool.replace(href).split(",");
        int leng = titles.length;
        for (int i = 0; i < leng; i++) {
            NavModel navModel = new NavModel();
            navModel.setTitle(titles[i]);
            navModel.setHref(hrefs[i]);
            list.add(navModel);
        }
        return list;
    }


}
